package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jshjsh on 2016/8/17.
 */
@Service
public abstract class PageFactoryExtend extends PageFactory{

    @Override
    public void packageContent(Elements main, List<PageTwo> pageTwos, List<PageTwoContent> contents, String category) {
        List<String> times = getTimes(main);
        Elements a = main.select("a");
        int size = a.size();
        for (int i = 0; i < size; i++) {
            Elements aElement = a.eq(i);
            String time = times.get(i);
            String abs_url = aElement.attr("abs:href");
            String url = aElement.attr("href");
            String title = aElement.text();
            Document document1 = WebsiteUtil.getDocByURL(abs_url);
            PageTwo pageTwo = dealTitle(url, title);
            pageTwo.setDate(time);
            pageTwo.setCategory(category);
            String key = pageTwo.getKey();
            Elements elements = getOtherMain(document1);
            filterAgain(elements);
            filterMain(elements);
            dealPhotoAndFile(elements);
            PageTwoContent content = getContent(elements);
            content.setKey(key);
            if (!content.getContent().equals("")) {
                contents.add(content);
                pageTwos.add(pageTwo);
            }
        }
    }

    /**
     * 如果有需要请重写,否则无法执行(而且使用时请务必重写packageContent方法且什么也不干)
     * @return
     */
    public abstract List<String> getTimes(Elements elements);




    public abstract PageTwo dealTitle(String url, String title);



}
